<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/logo.css">
    <script src="../js/axios.min.js"></script>
</head>

<body>
    <div id="main">
        <h1>欢迎登陆YSL官网</h1>
        <p class="con"><span>用户名：</span><input type="text" name="" id="" /><br/></p>
        <p class="con"><span>密码：</span><input type="password" name="" id="" /><br/></p>
        <p class="box">
            <input type="button" name="" id="" value="登录" />
            <a href="./register.html">
                <input type="button" name="" id="" value="注册" />
            </a>
            <a href="./index.html">
                <input type="button" name="" id="" value="返回首页" />
            </a>

        </p>
    </div>
    <script>
        let username = localStorage.getItem("username");
        if (username) {
            alert("已经登录了");
            location.href = "index.html";
        }
        let ipts = document.querySelectorAll("input");
        ipts[2].onclick = function() {
            let uname = ipts[0].value;
            let password = ipts[1].value;
            let loginAPI = "http://jx.xuzhixiang.top/ap/api/login.php";
            axios
                .get(loginAPI, {
                    params: {
                        username: uname,
                        password: password,
                    },
                })
                .then((res) => {
                    console.log(res.data);
                    alert(res.data.msg);
                    if (res.data.code == 1) {
                        localStorage.setItem("username", uname);
                        localStorage.setItem("id", res.data.data.id);
                        localStorage.setItem("token", res.data.data.token);
                        location.href = "index.html";
                    }
                });
        };
    </script>
</body>

</html>